// 定义 Swagger 属性的接口
export interface SwaggerProperty {
  type?: string;
  description?: string;
  items?: {
    type?: string;
    $ref?: string;
  };
  minimum: number;
  maximum: number;
  properties?: Record<string, SwaggerProperty>;
  $ref?: string;
}

export function formatRefTypeName(ref: string): string {
  // 从 #/definitions/vo.Xxx 提取并格式化类型名
  const name = ref.split("/").pop() || "";
  // 将 vo.Xxx 转换为 VoXxx
  return name
    .split(".")
    .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
    .join("");
}

export function convertToTypeScript(
  properties: Record<string, SwaggerProperty>,
  interfaceName: string,
  definitions?: any,
  processedTypes: Set<string> = new Set()
): string {
  // 如果该类型已经处理过，直接返回空字符串避免重复定义
  if (processedTypes.has(interfaceName)) {
    return "";
  }

  // 将当前类型添加到已处理集合中
  processedTypes.add(interfaceName);

  let result = `<span class="ts-keyword">interface</span> <span class="ts-type">${interfaceName}</span> <span class="ts-punctuation">{</span>\n`;

  for (const [key, value] of Object.entries(properties)) {
    let tsType: string;
    switch (value.type) {
      case "string":
        tsType = "string";
        break;
      case "integer":
      case "number":
        tsType = "number";
        break;
      case "boolean":
        tsType = "boolean";
        break;
      case "array":
        if (value.items?.$ref) {
          const refType = formatRefTypeName(value.items.$ref);
          tsType = `${refType}[]`;

          if (definitions) {
            const refName = value.items.$ref.split("/").pop() || "";
            const refProperties = definitions[refName]?.properties;
            if (refProperties) {
              const nestedInterface = convertToTypeScript(
                refProperties,
                refType,
                definitions,
                processedTypes
              );
              if (nestedInterface) {
                result = `${nestedInterface}\n${result}`;
              }
            }
          }
        } else {
          tsType = `${value.items?.type || "any"}[]`;
        }
        break;
      case "object":
        if (value.properties) {
          const nestedName = `${interfaceName}${key
            .charAt(0)
            .toUpperCase()}${key.slice(1)}`;
          const nestedInterface = convertToTypeScript(
            value.properties,
            nestedName,
            definitions,
            processedTypes
          );
          if (nestedInterface) {
            result = `${nestedInterface}\n${result}`;
          }
          tsType = nestedName;
        } else {
          tsType = "Record<string, any>";
        }
        break;
      default:
        if (value.$ref) {
          tsType = formatRefTypeName(value.$ref);
          if (definitions) {
            const refName = value.$ref.split("/").pop() || "";
            const refProperties = definitions[refName]?.properties;
            if (refProperties) {
              const nestedInterface = convertToTypeScript(
                refProperties,
                tsType,
                definitions,
                processedTypes
              );
              if (nestedInterface) {
                result = `${nestedInterface}\n${result}`;
              }
            }
          }
        } else if (!value.type && (value.minimum || value.maximum)) {
          tsType = "number";
        } else {
          tsType = "any";
        }
    }

    // 添加属性和注释
    result += `  <span class="ts-property">${key}</span><span class="ts-operator">:</span> <span class="ts-type">${tsType}</span><span class="ts-operator">;</span>`;
    if (value.description) {
      result += ` <span class="ts-comment">// ${value.description.replace(
        /\n/g,
        " "
      )}</span>`;
    }
    result += "\n";
  }

  result += "}\n";
  return result;
}
